<template>
    <div class="event-contain">
        <van-icon name="arrow-left" class="back-icon" @click="backPath()"/>
        <div class="event-info" v-if="eventNodeInfo.numList?.length">
            <div class="info-title">
                {{ eventNodeInfo.title }}情况
            </div>
            <div class="info-num flex-sb">
                <div class="num-item" v-for="item in eventNodeInfo.numList">
                    <div class="item-num">{{ item.value }}</div>
                    <div class="item-name">{{ item.name }}</div>
                </div>
            </div>
        </div>
        <div class="event-table">
            <table v-if="eventNodeInfo.allList?.length">
                <thead>
                    <tr class="tab-header">
                        <th>序号</th>
                        <th>楼室号</th>
                        <th>姓名</th>
                        <th>赞成/反对</th>
                        <th>留言</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item, index in eventNodeInfo.allList" :key="item.id">
                        <td>{{ index + 1 }}</td>
                        <td>{{ item.type }}</td>
                        <td>{{ item.create_user_truename }}</td>
                        <td>
                            <span :class="item.mark == '赞成' ? 'succ-tab' : 'tab'">{{ item.mark }}</span>
                        </td>
                        <td>{{ item.txt }}</td>
                    </tr>
                </tbody>
            </table>
            <div style="text-align: center;" v-if="!eventNodeInfo.allList?.length && !eventNodeInfo.listLoading">暂无数据
            </div>
            <van-loading color="#409EFF" style="text-align: center;" v-if="eventNodeInfo.listLoading"
                size="24px">加载列表中...</van-loading>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed, watch } from 'vue';
import { useConstValue, useLocalValue, useIuserInfoStore, useNodeValue } from '@/store/users.ts';
import { eventById, eventnodeuserApi, flowById, eventlogApi, iuserApi, nodeById, eventnodeuserCount } from '@/service/api.ts';
import { useRoute, useRouter } from 'vue-router';
const router = useRouter();
const nodeValue = useNodeValue();
const eventNodeInfo = reactive({
    title:'',
    type: '',
    numList: [],
    allList: [],
    listLoading: true
})
const getEventnodeuserList = (event) => {
    eventnodeuserApi({
        method: 'get',
        params: {
            event: event.eventID,
            node: event.nodeId,
        }
    }).then(res => {
        eventNodeInfo.listLoading = false;
        eventNodeInfo.allList = res?.results || [];
    }).catch(err => {
        eventNodeInfo.listLoading = false;
        console.log(err);
    })
}

watch(nodeValue, (newV, oldV) => {
    if (newV.eventID) {
        eventNodeInfo.title = newV.title;
        eventNodeInfo.type = newV.typeObj.type;
        let allRoles = newV.typeObj.type.split(',')
        let allNum = 0
        allRoles.forEach((item)=>{
            allNum += newV.typeObj.typeObj[item] * 1
        })
        let successNum = newV.nodeuserCount[0]['赞成'] * 1
        let noVoteNum = newV.nodeuserCount[0]['反对'] * 1
        let allPushNum = successNum + noVoteNum;
        console.log(allNum, allPushNum, successNum, noVoteNum);
        eventNodeInfo.numList = [
            { name: '总人数', value: allNum },
            { name: '已投票', value: allPushNum },
            { name: '未投票', value: allNum - allPushNum },
            { name: '赞成票', value: successNum },
            { name: '反对票', value: noVoteNum }
        ];
        getEventnodeuserList(newV)
    }
}, {
    deep: true,
    immediate: true,
})
const backPath = () => {
    router.back();
}
</script>

<style lang="scss" scoped>
.event-contain {
    background-color: #F7F7F7;
    height: 100vh;
    width: 100vw;
    overflow: auto;
}

.event-table {
    margin: 8px 12px;
    background-color: white;
    border-radius: 4px;
    padding: 12px;

    .succ-tab,
    .tab {
        display: inline-block;
        padding: 2px 10px;
        background-color: #FEF0F0;
        border: 2px solid #FDE2E2;
        color: #F56C6C;
        line-height: 20px;
        border-radius: 2px 2px 2px 2px;
    }

    .succ-tab {
        background-color: #ECF5FF;
        border-color: #D9ECFF;
        color: #409EFF;
    }

    table {
        width: 100%;
        font-size: 14px;
        text-align: left;
        table-layout: fixed;
        border-collapse: collapse;

        thead {
            background-color: #F7F7F7;
        }

        tbody {
            tr {
                border-bottom: 1px solid #E5E6EB;
            }

            td:first-child {
                padding-left: 6px;
            }

            td {
                padding: 12px 0;
                font-weight: 400;
                word-wrap: break-word;

            }

            td:last-child {
                padding-right: 10px;
            }
        }
    }

    .tab-header {
        th:first-child {
            width: 40px;
            padding-left: 6px;
        }

        th {
            width: calc((100% - 46px) /4);
            padding: 12px 0;
            color: #111111;
            font-weight: 500;
        }
    }
}
.back-icon{
    margin: 8px 12px;
    border-radius: 4px;
    color: #1F89FF;
    font-size: 22px;
}
.event-info {
    margin: 8px 12px;
    background-color: white;
    border-radius: 4px;
    color: #111111;

    .info-title,
    .info-num {
        padding: 12px;
        font-size: 16px;
    }

    .num-item {
        text-align: center;
    }

    .item-num {
        font-size: 18px;
    }

    .item-name {
        font-size: 14px;
        color: #666666;
    }
}
</style>